﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using AutoMapper;
using H2.NET.Common;
using H2.NET.DBUtility;
using H2.NET.Web.Pages.SMD.Model;
using H2.NET.Web.Workflow.DwzCode;

namespace H2.NET.Web.Pages.SMD
{
    public partial class StoreList : BasePage
    {
        public string AuthId = string.Empty;
        public string StoreName = string.Empty;
        public string EapproveSn = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            AuthId = Request.Form["AuthId"];
            EapproveSn = Request.Form["EapproveSn"];
            StoreName = Request.Form["StoreName"];

            PageNumber = Convert.ToInt32(Request.Form["pageNum"] ?? "1");

            var stores = new List<t_smd_Store>();

            var strwhere = "1=1";
            if (!string.IsNullOrEmpty(AuthId))
            {
                strwhere += " and authid like '%" + AuthId + "%'";
            }
            if (!string.IsNullOrEmpty(StoreName))
            {
                strwhere += " and StoreName like '%" + StoreName + "%'";
            }
            if (!string.IsNullOrEmpty(EapproveSn))
            {
                strwhere += " and EapproveSn like '%" + EapproveSn + "%'";
            }

            var builder = new StringBuilder();
            builder.Append("SELECT TOP " + PageSize + " * FROM ( ");
            builder.Append("SELECT ROW_NUMBER() OVER (ORDER BY StoreID) AS RowNumber,* FROM t_smd_Store ");
            builder.Append("WHERE " + strwhere);
            builder.Append(") A WHERE RowNumber > " + PageSize + "*(" + PageNumber + "-1)");

            DbHelperSQL.connectionString = PubConstant.GetConnectionString("smdDB");
            using (SqlDataReader reader = DbHelperSQL.ExecuteReader(builder.ToString()))
            {
                while (reader.Read())
                {
                    stores.Add(Mapper.DynamicMap<t_smd_Store>(reader));
                }
            }

            TotalItemCount = Utils.StrToInt(DbHelperSQL.GetSingle("select count(1) from t_smd_Store where " + strwhere).ToString(), 0);

            Pager1.TotalItemCount = TotalItemCount;
            Pager1.PageSize = PageSize;
            Pager1.PageNumber = PageNumber;
            rpt_stores.DataSource = stores;
            rpt_stores.DataBind();
        }
        protected void rpt_stores_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                var lBusinesskeys = e.Item.FindControl("l_businesskeys") as Literal;

                if (string.IsNullOrEmpty(lBusinesskeys.Text)) return;

                var strBusinesskeys = string.Empty;
                var businesskeys = FindActiveBusinessProcess(lBusinesskeys.Text);
                strBusinesskeys = businesskeys.Aggregate(strBusinesskeys,
                    (current, businessKey) =>
                        current + (businessKey + ","));

                lBusinesskeys.Text = strBusinesskeys.Trim(',');
            }
        }

        private IEnumerable<string> FindActiveBusinessProcess(string authid)
        {
            var businessKeys = new List<string>();
            var sql =
                    "SELECT DISTINCT a.BusinessKey FROM [t_smd_ChildProcess] a " +
                    "INNER JOIN [t_smd_Relation] b on a.BusinessKey = b.BusinessKey " +
                    "WHERE a.Status = 'active' and (AuthID = '" + authid + "' or AuthID like '%," + authid +
                    "%' or AuthID like '%" + authid + ",%')";

            DbHelperSQL.connectionString = PubConstant.GetConnectionString("oleDB");
            using (SqlDataReader reader = DbHelperSQL.ExecuteReader(sql))
            {
                while (reader.Read())
                {
                    businessKeys.Add(reader.GetString(0));
                }
            }

            return businessKeys;
        }
    }
}